package com.fick.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

/**
 * 
 * 
 * 
 * Title: �ļ��ϴ���
 * 
 * Description: ���ܶ��ļ������ϴ�,����ȡ��������ֵ,����ͬʱ�ϴ�255���ļ�
 * 
 * Copyright: Copyright (c) 2002
 * 
 * Company: Tekson
 * 
 * @author Ī��
 * 
 * @version 1.0
 * 
 */

public class UpPic {
	public static void main(String[] args) {
		// System.out.println("Test OK");
	}

	private String[] sourceFile = new String[255]; // Դ�ļ���

	private String[] suffix = new String[255]; // �ļ���׺��

	private String canSuffix = ".gif.jpg.jpeg.png"; // ���ϴ����ļ���׺��

	private String objectPath = "c:/"; // Ŀ���ļ�Ŀ¼

	private String[] objectFileName = new String[255]; // Ŀ���ļ���

	private ServletInputStream sis = null; // ������

	private String[] description = new String[255]; // ����״̬

	private long size = 100 * 1024; // ���ƴ�С

	private int count = 0; // �Ѵ����ļ���Ŀ

	private byte[] b = new byte[4096]; // �ֽ���������

	private boolean successful = true;

	private Hashtable fields = new Hashtable();

	// �����ϴ��ļ��ĺ�׺��

	public UpPic() {

	}

	// �ж��ϴ��ļ�������
	private boolean canTransfer(int i) {
		suffix[i] = suffix[i].toLowerCase();
		// �������4��ͼƬ�ģ���λ���԰Ѻ�׺��ĵ���߲�Ҫ������
		if (sourceFile[i].equals("") || (!(canSuffix.indexOf("." + suffix[i]) >= 0))) {
			description[i] = "ERR: File suffix is wrong.";
			return false;
		} else {
			return true;
		}
	}

	// ȡ���ϴ��ļ���
	public int getCount() {
		return count;
	}

	// ȡ���ϴ�״̬����
	public String[] getDescription() {
		return description;
	}

	// ȡ�ñ?Ԫ��ֵ
	public String getFieldValue(String fieldName) {
		if (fields == null || fieldName == null) {
			return null;
		}
		return (String) fields.get(fieldName);
	}

	// ȡ��Ŀ���ļ���
	public String[] getObjectFileName() {
		return objectFileName;
	}

	// ȡ��Ŀ��·��
	public String getObjectPath() {
		return objectPath;
	}

	// ȡ��Դ�ļ���
	public String[] getSourceFile() {
		return sourceFile;
	}

	// �����ļ�����·��
	public void setObjectPath(String objectPath) {
		this.objectPath = objectPath;
	}

	// �����ļ�����·��
	public void setSize(long maxSize) {
		this.size = maxSize;
	}

	// �ļ��ϴ��������
	public void setSourceFile(HttpServletRequest request) throws IOException {
		sis = request.getInputStream();
		int a = 0;
		int k = 0;
		String s = "";
		while ((a = sis.readLine(b, 0, b.length)) != -1) {
			s = new String(b, 0, a);
			if ((k = s.indexOf("filename=\"")) != -1) {
				s = s.substring(k + 10);
				k = s.indexOf("\"");
				s = s.substring(0, k);
				sourceFile[count] = s;
				k = s.lastIndexOf(".");
				suffix[count] = s.substring(k + 1);
				if (canTransfer(count)) {
					transferFile(count, s.substring(s.lastIndexOf("\\") + 1, s.lastIndexOf(".")));
				}
				++count;

			} else if ((k = s.indexOf("name=\"")) != -1) {
				String fieldName = s.substring(k + 6, s.length() - 3);
				sis.readLine(b, 0, b.length);
				StringBuffer fieldValue = new StringBuffer(b.length);
				while ((a = sis.readLine(b, 0, b.length)) != -1) {
					s = new String(b, 0, a - 2);
					if ((b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
						break;
					} else {
						fieldValue.append(s);
					}
				}
				if (fields.containsKey(fieldName)) {
					fields.put(fieldName, fields.get(fieldName) + "," + fieldValue.toString()); // 如果得压入时已有该键值，将已有值加上现得值放入集合。
				} else {
					fields.put(fieldName, fieldValue.toString());
				}

			}
			if (!successful) {
				break;
			}
		}
	}

	public void setSuffix(String canSuffix) {
		this.canSuffix = canSuffix;
	}

	// �ϴ��ļ�ת��
	private void transferFile(int i, String filename) {
		// String x = Long.toString(new java.util.Date().getTime());
		try {
			// objectFileName[i] = x + "." + suffix[i];
			objectFileName[i] = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "." + suffix[i];
			FileOutputStream out = new FileOutputStream(objectPath + objectFileName[i]);
			int a = 0;
			int k = 0;
			long hastransfered = 0; // ��ʾ�Ѿ�������ֽ���
			String s = "";
			while ((a = sis.readLine(b, 0, b.length)) != -1) {
				s = new String(b, 0, a);
				if ((k = s.indexOf("Content-Type:")) != -1) {
					break;
				}
			}
			sis.readLine(b, 0, b.length);
			while ((a = sis.readLine(b, 0, b.length)) != -1) {
				// s = new String(b, 0, a);
				if ((b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
					break;
				}
				out.write(b, 0, a);
				hastransfered += a;
				if (hastransfered >= size) {
					description[count] = "ERR: The file " + sourceFile[count]
							+ " is too large to transfer. The whole process is interrupted.";
					successful = false;
					break;
				}
			}
			if (successful) {
				description[count] = "Right: The file " + sourceFile[count] + " has been transfered successfully.";
			}
			out.flush();
			out.close();
			if (!successful) {
				sis.close();
				File tmp = new File(objectPath + objectFileName[count]);
				tmp.delete();
			}

		} catch (IOException ioe) {
			description[i] = ioe.toString();
		}
	}
}
